<?php
class Person extends AppModel{
	var $name = 'Person';
				
	// Define the join relation between a person and family
	var $hasAndBelongsToMany = array('Family');
				
	/* 
	 * Returns a boolean value indicating whether user with id $uid is an admin of event with id $eid
	 * 
	 */
	function isEventAdmin($uid, $eid) {
		$event = ClassRegistry::init('Event');
				
		$fams = $this->query("SELECT family_id FROM families_people WHERE person_id=$uid AND admin=1");
						
		// Clean the query format up, we just need the fam ids
		$admin_fams = array();
		foreach ($fams as $fam) {
			$admin_fams[] = $fam['families_people']['family_id'];
		}
						
		// Get event host family
		$host = $this->query("SELECT family_id from events WHERE id=$eid");
		$ev_host = $host[0]['events']['family_id'];

		return in_array($ev_host, $admin_fams);
	}
				
	/*
	 * Returns a boolean value indicating whether the user with id $uid has privilege to view event with id $eid
	 * 
	 */
	function canViewEvent($pid = null, $evid = null) {
		$family = ClassRegistry::init('Family');
				
		$fams = $this->getFamiliesFromPerson($pid);
		$evs = $family->getEventsFromFamilies($fams);
						
		return in_array($evid, $evs);
	}
				
	/*
	 * Return an array of the families that a given person is a member of
	 *
	 * **returns an empty array if no pid is entered, to avoid a warning in app_controller**
	 */
	function getFamiliesFromPerson($pid = null) {
		if ($pid == null)
			return array();
		$fams = $this->find('all', array('conditions' => array('Person.id' => $pid), 'order' => 'Person.id ASC'));
		$fams = $fams[0]['Family'];
						
		$returnFamilies = array();
		// Add each family to the list
		foreach($fams as $fam) {
			$returnFamilies[] = array('id' => $fam['id'], 'name' => $fam['name']);
		}
						
		return $returnFamilies;
	}
				
	/*
	 * Return all fields of a person from their ID
	 *
	 */		
	function getPersonFromID($pid) {
		$result = $this->find('first', array('conditions' => array('Person.id' => $pid),
					 'fields' => array('Person.id', 
								 'Person.first_name', 
								 'Person.last_name',
								 'Person.username', 
								 'Person.created')));
		return $result['Person'];
	}
	
	
	/*
	 * Returns and array of persons from array of IDs
	 *
	 */		
	function getPeopleFromIDs($pids) {
		$result = array();
		
		foreach ($pids as $pid) {
			$result[$pid] = $this->getPersonFromID($pid);
		}
		
		return $result;
	}
		
		function getPersonUsername($pid) {
			$result = $this->find('first', array('conditions' => array('Person.id' => $pid),
										      'fields' => array('Person.username')));
			return $result;
		}
		
		// The returned array is indexed by user ids
		function getPeopleUsernames($pids) {
			$result = array();
			
			foreach ($pids as $pid) {
				$person = $this->find('first', array('conditions' => array('Person.id' => $pid),
												     'fields' => array('Person.username')));
				$result[$pid] = "{$person['Person']['username']}";
			}
			
			return $result;
		}
		
		function getPersonIdFromUsername($username) {
			$result = $this->find('first', array('conditions' => array('Person.username' => $username),
										         'fields' => array('Person.id')));
			
			//$result = $this->query("SELECT id FROM people WHERE username='$username'");
			return $result['Person']['id'];
		}

	/*
	 * Validation for the user registration
	 */
	var $validate = array(
			'first_name' => array(
								'between' => array(
								 'rule' => array('between', 1, 40),
								 'last' => true,
								 'required' => true,
								 'allowempty' => false,
								 'message' => 'First name must be between 1 and 40 characters'
								 )
								),
			'username' => array(
							'email' => array(
									 'rule' => array('email', true),
									 'required' => true,
									 'allowempty' => false,
									 'message' => 'Must be a valid email address'
									 ),
							'isUnique' => array(
								'rule' => 'isUnique',
								'message' => 'This username has been taken.'
								)
							),
			'password' => array(
							'rule' => array('minLength', 0),
							'required' => true,
							),
			'password_confirm' => array(
								'rule' => array('minLength', 5),
								'required' => true,
								'message' => 'Password must be at least 5 characters'
								), 
			'password_old' => array(
						'rule' => array('required' => true)
						)
			);
	}
?>
